package com.ydcycloud.company.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mvc.model.Pagination;
import com.smart.mvc.model.Result;
import com.smart.mvc.validator.Validator;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.util.StringUtils;
import com.ydcycloud.company.model.Trade;
import com.ydcycloud.company.service.TradeService;
import com.ydcycloud.sys.controller.BaseAdminController;

@Controller
@RequestMapping("/admin/trade")
public class TradeController extends BaseAdminController {

	@Resource
	private TradeService tradeService;

	/**
	 * 跳转到行业列表页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping
	public String excute(Model model) {
		return "/admin/trade/tradeList";
	}

	/**
	 * 企业列表
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(path = "/list")
	public @ResponseBody
	Result list(
			@ValidateParam(name = "开始页码", validators = { Validator.NOT_BLANK }) Integer pageNo,
			@ValidateParam(name = "显示条数 ", validators = { Validator.NOT_BLANK }) Integer pageSize) {

		Pagination<Trade> p = new Pagination<>(pageNo, pageSize);
		return Result.createSuccessResult().setData(
				tradeService.findPagination(p));
	}
	
	/**
	 * 删除行业
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/delete")
	public @ResponseBody Result delete(@ValidateParam(name="主键ids")String ids){
		if(StringUtils.isBlank(ids)){
			return Result.createErrorResult().setMessage("至少选择一行");
		}
		tradeService.deleteByIds(ids);
		return Result.createSuccessResult().setMessage("删除成功");
	}
	
	/**
	 * 跳转到编辑/新增行业页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/edit")
	public String edit(Integer id, Model model) {
		if (id != null) {
			Trade bean = tradeService.get(id);
			model.addAttribute("bean", bean);
		}
		//所有父目录
		model.addAttribute("list", tradeService.findListByProperty("id", 0));
		return "/admin/trade/tradeEdit";
	}
	
	/**
	 * 保存编辑行业
	 * @param request
	 * @param id
	 * @param tradeName
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody
	Result save(HttpServletRequest request, Integer id, String tradeName) {
		
		if(StringUtils.isBlank(tradeName)){
			return Result.createErrorResult().setMessage("行业名称不能为空");
		}
		Trade bean = null;
		if (id != null) {
			bean = tradeService.get(id);
		} else {
			bean = new Trade();
			bean.setCreateBy(getSessionAdmin(request.getSession()).getName()); // 创建人
			bean.setCreateTime(new Date()); // 创建时间
		}
		bean.setTradeName(tradeName);
		bean.setLastUpdateTime(new Date());
		tradeService.save(bean);
		return Result.createSuccessResult().setData(bean);
	}

}
